<?php
Yii::import('zii.widgets.jui.CJuiInputWidget');
class JMaterialModelSelector extends CJuiInputWidget {

	public $unitInputId = null;
	public $unitTextId = null;
	/**
	 * JS string: function(model){ /// }
	 */
	public $onSelectCallback = null;

	public function init() {
		parent::init();
		$assetsUrl = Yii::app()->assetManager
			->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets', false, -1, YII_DEBUG);
		$cs = Yii::app()->getClientScript();
		$cs->registerScriptFile($assetsUrl . '/jx.material.js');
	}

	public function run() {
		list($name, $id) = $this->resolveNameID();
		if (isset($this->htmlOptions['id']))
			$id = $this->htmlOptions['id'];
		else
			$this->htmlOptions['id'] = $id;

		$materialModelId = $this->value;

		if ($this->hasModel()) {
			$attribute = $this->attribute;
			CHtml::resolveName($this->model, $attribute);
			$materialModelId = $this->model->{$attribute};
			echo CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
		} else {
			echo CHtml::hiddenField($name, $this->value, $this->htmlOptions);
		}

		$Unselectedlabel = $label = '未选择物品';
		if ($materialModelId) {
			$materialModel = MaterialModel::model()->findByPk($materialModelId);
			if ($materialModel) {
				$label = $materialModel->getModelName();
			}
		}

		$label = CJSON::encode($label);

		$inputJs = $this->unitInputId ? "if(!jQuery('#{$this->unitInputId}').val()){jQuery('#{$this
				->unitInputId}').val(model.unit);}" : '';
		if ($this->unitTextId) {
			$inputJs .= "
			;jQuery('#{$this->unitTextId}').html(model.unit);";
		}
		if ($this->onSelectCallback) {
			if ($this->unitTextId) {
				$inputJs .= ";({$this->onSelectCallback})(model);";
			}
		}
		$js = <<<EOF
Ext.onReady(function(){
	new Ext.Button({
		renderTo: '$id-container',
		text: $label,
		handler: function(button){
			JX.Material.chooseModel(function(material,model){
				if(material){
					button.setText(material.name+' '+model.model);
					jQuery('#$id').val(model.id);
					$inputJs
				}else{
					button.setText('$Unselectedlabel');
					jQuery('#$id').val('');
				}
			});
		}
	});
});				
				
EOF
		;

		$cs = Yii::app()->getClientScript();
		$cs->registerScript(__CLASS__ . '#' . $id, $js, CClientScript::POS_END);
		if (!$cs->isScriptRegistered(__CLASS__, CClientScript::POS_END)) {
			$modelSearchUrl = CJSON::encode(
				Yii::app()->getController()->createUrl('materialModel/search'));
			$materialSearchUrl = CJSON::encode(
				Yii::app()->getController()->createUrl('material/search'));
			$materialCreateUrl = CJSON::encode(
					Yii::app()->getController()->createUrl('material/create'));
			$materialModelManageUrl = CJSON::encode(
					Yii::app()->getController()->createUrl('material/updateModels',array('id'=>'__MaterialId__')));
			$updateMaterialRequisitionHistoryUrl = CJSON::encode(
				Yii::app()->getController()->createUrl('materialRequisition/searchHistory'));
			$updateMaterialWarehouseInboundHistoryUrl = CJSON::encode(
				Yii::app()->getController()->createUrl('materialWarehouseInbound/searchHistory'));
			$updateMaterialReceiptHistoryUrl = CJSON::encode(
				Yii::app()->getController()->createUrl('materialReceipt/searchHistory'));

			$cs
				->registerScript(__CLASS__,
					<<<EOF
JX.Material.modelSearchUrl=$modelSearchUrl;
JX.Material.materialSearchUrl=$materialSearchUrl;
JX.Material.materialCreateUrl=$materialCreateUrl;
JX.Material.materialModelManageUrl=$materialModelManageUrl;
JX.Material.updateMaterialWarehouseInboundHistoryUrl=$updateMaterialWarehouseInboundHistoryUrl;
JX.Material.updateMaterialRequisitionHistoryUrl=$updateMaterialRequisitionHistoryUrl;
JX.Material.updateMaterialReceiptHistoryUrl=$updateMaterialReceiptHistoryUrl;
EOF
					, CClientScript::POS_READY);
		}
		echo CHtml::tag('div', array(
			'id' => $id . '-container'
		), '');
	}
}
